package com.free.semantic.web.configure;

import com.free.semantic.web.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private AuthInterceptor authInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 1.对所有请求路径格式进行跨域处理
                .allowedHeaders("*")        // 2.允许任意请求头
                .allowedMethods("*")        // 3.允许任意请求方法 get post head delete put
                .maxAge(1800)               // 4.设置跨域有效期
                .allowedOrigins("http://localhost:63342"); // 5.支持的域名
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                    "/api/auth/**",  // 登录注册接口
                    "/user/**",      // 用户相关页面
                    "/static/**",    // 静态资源
                    "/",            // 首页
                    "/index.html",  // 首页
                    "/error"        // 错误页面
                );
    }
}